package com.zxx.casserver.modules.permission.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxx.cascommon.base.exception.ParamsException;
import com.zxx.cascommon.base.result.Result;
import com.zxx.cascommon.base.result.ResultUtils;
import com.zxx.casserver.modules.permission.entity.PermissionDO;
import com.zxx.casserver.modules.permission.entity.PermissionVO;
import com.zxx.casserver.modules.permission.service.IPermissionService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 权限菜单管理-前端控制器
 * </p>
 *
 * @author zhoukai
 * @since 2020-08-03
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    private final IPermissionService permissionService;

    public PermissionController(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @PostMapping("/list")
    public Result listApp(@RequestBody PermissionVO permissionVO) {
        IPage<PermissionDO> permissionPage = permissionService.listPermission(permissionVO);
        if (ObjectUtils.isEmpty(permissionPage)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(permissionPage);
    }

    @PostMapping("/save")
    public Result savePermission(@RequestBody PermissionVO permissionVO) {
        if (ObjectUtils.isEmpty(permissionVO.getAppId())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(permissionVO.getName())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(permissionVO.getType())) {
            throw new ParamsException();
        }
        PermissionDO permissionDO = permissionService.savePermission(permissionVO);
        if (ObjectUtils.isEmpty(permissionDO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(permissionDO);
    }

    @PostMapping("/update")
    public Result updatePermission(@RequestBody PermissionVO permissionVO) {
        if (ObjectUtils.isEmpty(permissionVO.getPermissionId())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(permissionVO.getAppId())) {
            throw new ParamsException();
        }
        PermissionDO permissionDO = permissionService.updatePermission(permissionVO);
        if (ObjectUtils.isEmpty(permissionDO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(permissionDO);
    }


    @PostMapping("/remove")
    public Result removePermission(@RequestBody PermissionVO permissionVO) {
        if (ObjectUtils.isEmpty(permissionVO.getPermissionId())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(permissionVO.getAppId())) {
            throw new ParamsException();
        }
        PermissionDO permissionDO = permissionService.removePermission(permissionVO);
        if (ObjectUtils.isEmpty(permissionDO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(permissionDO);
    }

}

